iT邦幫忙

2021 iThome 鐵人賽

DAY 9
0
Mobile Development

就是從無到有寫app系列 第 9

第9天~接續第2頁-

  • 分享至 

  • xImage
  •  

接續第2頁-
可參考什麼是Intent意圖? https://litotom.com/ch5-2-intent/
//打包資料
建立一個包裹然後塞東西
bundle.putString("name",name);就是map的寫法:key和value

https://ithelp.ithome.com.tw/upload/images/20220131/20119035v5W3s69y96.png

//建立Intent物件
        Intent it = new Intent();//送貨員
        it.setClass(MainActivity.this,Page2.class);//送到哪裡去

        //打包資料
        Bundle bundle = new Bundle();
        bundle.putString("name",name);
        bundle.putString("phone",phone);
        bundle.putInt("num",num);
        bundle.putString("meal",mealString);
        bundle.putString("drink",drinkString);
        bundle.putString("memo",all);
        //Intent夾帶資料包裹
        it.putExtras(bundle);

        //送到第2頁
        startActivity(it);//出發了

目前第2頁還空白的

https://ithelp.ithome.com.tw/upload/images/20220131/20119035cLcg69raI2.png


第2頁還空白的程式碼;

刪掉androidx.constraintlayout.widget.ConstraintLayout改成LinearLayout

https://ithelp.ithome.com.tw/upload/images/20220131/20119035ylpQrevbQ0.png

https://ithelp.ithome.com.tw/upload/images/20220131/20119035RCfTowrR3D.png

LinearLayout-丟進來就可以依序排列.看到xml檔

https://ithelp.ithome.com.tw/upload/images/20220131/20119035IZWBB81J0n.png


開始放入6個文字欄位textView+加入2個按鈕

https://ithelp.ithome.com.tw/upload/images/20220131/20119035gmiMmMHxGW.png

要美化..加入內距不可以放東西-padding-30dp

https://ithelp.ithome.com.tw/upload/images/20220131/20119035SmC4R3pu6U.png


改字的大小20sp一起選可以一起改
https://ithelp.ithome.com.tw/upload/images/20220131/2011903503V2K4L4HH.png


用layout_margin Top分開
https://ithelp.ithome.com.tw/upload/images/20220131/20119035dijVOZmMzQ.png

button也要分開-
https://ithelp.ithome.com.tw/upload/images/20220131/20119035uuciDWYI6O.png


button要編輯文字+onClick

https://ithelp.ithome.com.tw/upload/images/20220131/20119035Q0qupa5xVK.png

回java程式碼onClick-->用這個不用加id

https://ithelp.ithome.com.tw/upload/images/20220131/20119035zJVUW9b0kH.png

https://ithelp.ithome.com.tw/upload/images/20220131/201190350Pg23wKiAh.png

第2頁的初始化-

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);
        

    }

    public void onClick(View view) {
    }
}

接收包裹-迎接第一頁送過來的+用map的方式key和value

從put變get

key在程式碼不會顯示,所以用截圖

https://ithelp.ithome.com.tw/upload/images/20220131/20119035lqwnAAkm3v.png

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);

        //接收包裹
        Intent it2 =getIntent();
        Bundle bundle =it2.getExtras();

        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int num = bundle.getInt("num");
        String meal = bundle.getString("meal");
        String drink = bundle.getString("drink");
        String memo = bundle.getString("memo");
            }

    public void onClick(View view) {
    }
}

繼續編輯顯示:

https://ithelp.ithome.com.tw/upload/images/20220131/20119035yXOZrpPmyB.png

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);

        //接收包裹
        Intent it2 =getIntent();
        Bundle bundle =it2.getExtras();

        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int num = bundle.getInt("num");
        String meal = bundle.getString("meal");
        String drink = bundle.getString("drink");
        String memo = bundle.getString("memo");

        t1.setText("訂購人"+name);
        t2.setText(phone);
        t3.setText(num);
        t4.setText(meal);
        t5.setText(drink);
        t6.setText(memo);

        //小計
        


            }

    public void onClick(View view) {
    }
}

再來是小計:文字要去比對內容要用equals不要用=
用toast顯示就要用手機試

https://ithelp.ithome.com.tw/upload/images/20220131/20119035AxJSleCf5B.jpg

https://ithelp.ithome.com.tw/upload/images/20220131/201190352ywbhVZ2Ew.png

//小計
        int sum =0;
        if(meal.equals("雞肉")){
            sum =num * 80;
        }else if(meal.equals("豬肉")){
            sum =num * 85;
        }else if(meal.equals("X牛羊")){
            sum =num * 90;
        }else if(meal.equals("海鮮")){
            sum =num*70;
        }

再寫入toast顯示:

//小計
        int sum =0;
        if(meal.equals("雞肉")){
            sum =num * 80;
        }else if(meal.equals("豬肉")){
            sum =num * 85;
        }else if(meal.equals("X牛羊")){
            sum =num * 90;
        }else if(meal.equals("海鮮")){
            sum =num*70;
        }
        Toast.makeText(Page2.this,"小計:"+sum+"元",Toast.LENGTH_LONG).show();

目前完整的程式碼:

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);

        //接收包裹
        Intent it2 =getIntent();
        Bundle bundle =it2.getExtras();

        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int num = bundle.getInt("num");
        String meal = bundle.getString("meal");
        String drink = bundle.getString("drink");
        String memo = bundle.getString("memo");

        t1.setText("訂購人"+name);
        t2.setText(phone);
        t3.setText(num);
        t4.setText(meal);
        t5.setText(drink);
        t6.setText(memo);

        //小計
        int sum =0;
        if(meal.equals("雞肉")){
            sum =num * 80;
        }else if(meal.equals("豬肉")){
            sum =num * 85;
        }else if(meal.equals("X牛羊")){
            sum =num * 90;
        }else if(meal.equals("海鮮")){
            sum =num*70;
        }
        Toast.makeText(Page2.this,"小計:"+sum+"元",Toast.LENGTH_LONG).show();
    }

    public void onClick(View view) {
    }
}

再加上t7因為Toast會消失
https://ithelp.ithome.com.tw/upload/images/20220131/201190358QaK7ut2nt.png

java程式檔也要改

加入t7

https://ithelp.ithome.com.tw/upload/images/20220201/20119035xIL22hK7ou.png

原來的Toast

Toast.makeText(Page2.this,"小計:"+sum+"元",Toast.LENGTH_LONG).show();

改成-

t7.setText("小計:"+sum+"元");

跑一下

https://ithelp.ithome.com.tw/upload/images/20220201/20119035eCv4cLlS6T.png

目前是第2頁程式碼:

package com.huang.myapplication8;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

public class Page2 extends AppCompatActivity {
    TextView t1,t2,t3,t4,t5,t6,t7;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_page2);
        t1 =findViewById(R.id.t1);
        t2 =findViewById(R.id.t2);
        t3 =findViewById(R.id.t3);
        t4 =findViewById(R.id.t4);
        t5 =findViewById(R.id.t5);
        t6 =findViewById(R.id.t6);
        t7 =findViewById(R.id.t7);

        //接收包裹
        Intent it2 =getIntent();
        Bundle bundle =it2.getExtras();

        String name = bundle.getString("name");
        String phone = bundle.getString("phone");
        int num = bundle.getInt("num");
        String meal = bundle.getString("meal");
        String drink = bundle.getString("drink");
        String memo = bundle.getString("memo");

        t1.setText("訂購人"+name);
        t2.setText(phone);
        t3.setText(num);
        t4.setText(meal);
        t5.setText(drink);
        t6.setText(memo);


        //小計
        int sum =0;
        if(meal.equals("雞肉")){
            sum =num * 80;
        }else if(meal.equals("豬肉")){
            sum =num * 85;
        }else if(meal.equals("X牛羊")){
            sum =num * 90;
        }else if(meal.equals("海鮮")){
            sum =num*70;
        }

        t7.setText("小計:"+sum+"元");

        //Toast.makeText(Page2.this,"小計:"+sum+"元",Toast.LENGTH_LONG).show();
    }

    public void onClick(View view) {
    }
}

目前是第2頁模擬器跳不過去-後面更新應該會在第11天~




上一篇
第8天~
下一篇
第10天~生命週期
系列文
就是從無到有寫app31
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言